package cn.edu.klmy.controller;

import cn.edu.klmy.dao.IAuthorityDao;
import cn.edu.klmy.models.role.Role;
import cn.edu.klmy.models.role.RoleMember;
import cn.edu.klmy.models.teacher.Teacher;
import cn.edu.klmy.models.user.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import web.models.CommonResult;

import java.util.List;

/**
 * 角色控制器
 *
 * @author 顾伟
 *         创建日期：13-4-8
 */
@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private IAuthorityDao authorityDao;

    /**
     * 读取系统中所有角色
     *
     * @return
     */
    @RequestMapping("/list/all")
    @ResponseBody
    public List<Role> loadAllRoles() {
        return authorityDao.getAllRoles();
    }

    @RequestMapping("/{roleId}/list/member")
    @ResponseBody
    public List<RoleMember> loadRoleMember(@PathVariable int roleId) {
        return authorityDao.getRoleMember(roleId);
    }

    @RequestMapping("/list/notIn/{roleId}/member")
    @ResponseBody
    public List<Teacher> loadNonRoleMemberTeacher(@PathVariable int roleId){
        return authorityDao.getTeachersNotInRole(roleId);
    }

    @RequestMapping("/{roleId}/add/member/{memberId}")
    @ResponseBody
    public RoleMember addMemberToRole(@PathVariable int memberId,@PathVariable int roleId){
        return authorityDao.addMemberToRole(memberId,roleId);
    }

    @RequestMapping("/{roleId}/delete/member/{memberId}")
    @ResponseBody
    public boolean removeMemberFromRole(@PathVariable int memberId,@PathVariable int roleId){
        int count= authorityDao.deleteMemberFromRole(memberId,roleId);
        if(count==0){
            return false;
        }
        return true;
    }
}
